package com.oig.sys.workflow.controller;


import com.oig.common.exception.BadRequestException;
import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.log.annotation.SysLog;
import com.oig.sys.security.util.SecurityUtil;
import com.oig.sys.workflow.app.IAppAutoRuleService;
import com.oig.sys.workflow.dto.AutoRuleDto;
import com.oig.sys.workflow.vo.AutoRuleVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 自动化规则配置管理
 */
@Tag(name = "自动化规则配置管理")
@RestController
@RequestMapping("/autoRule")
@Slf4j
@RequiredArgsConstructor
public class TbAutoRuleController {

    private final IAppAutoRuleService appAutoRuleService;


    @PreAuthorize("@pms.hasPermission('/autoRule/add')")
    @SysLog(value="添加自动化规则配置",apiUrl="/autoRule/add")
    @Operation(summary = "添加自动化规则配置")
    @PostMapping(value = "/add")
    public AutoRuleVo addAutoRule(@RequestBody @Validated(GroupAdd.class) AutoRuleDto autoRuleDto){
        log.info("add auto rule config, {}", autoRuleDto);
        autoRuleDto.setTenantId(SecurityUtil.getDefineUserMain().getTenantId());
        autoRuleDto.setTenantName(SecurityUtil.getDefineUserMain().getTenantName());
        autoRuleDto.setCreateUserId(SecurityUtil.getDefineUserMain().getId());
        autoRuleDto.setCreateUserName(SecurityUtil.getDefineUserMain().getRealName());
        autoRuleDto.setCreateTime(new Date());
        autoRuleDto.setLastUpdateId(autoRuleDto.getCreateUserId());
        autoRuleDto.setLastUpdateName(autoRuleDto.getCreateUserName());
        autoRuleDto.setLastUpdateTime(autoRuleDto.getCreateTime());
        return this.appAutoRuleService.addAutoRule(autoRuleDto);
    }


    @PreAuthorize("@pms.hasPermission('/autoRule/update')")
    @SysLog(value="修改自动化规则配置",apiUrl="/autoRule/update")
    @Operation(summary = "修改自动化规则配置")
    @PostMapping(value = "/update")
    public AutoRuleVo updateAutoRule(@RequestBody @Validated(GroupEdit.class) AutoRuleDto autoRuleDto){
        log.info("update auto rule config, {}", autoRuleDto);
        this.checkRole(autoRuleDto.getId());
        autoRuleDto.setLastUpdateName(SecurityUtil.getDefineUserMain().getRealName());
        autoRuleDto.setLastUpdateId(SecurityUtil.getDefineUserMain().getId());
        autoRuleDto.setLastUpdateTime(new Date());
        return this.appAutoRuleService.updateAutoRule(autoRuleDto);
    }

    private void checkRole(Integer autoRuleId){
        AutoRuleVo autoRule = this.appAutoRuleService.getAutoRule(autoRuleId);
        if (autoRule==null){
            throw new BadRequestException("数据不存在");
        }
        SecurityUtil.getDefineUserMain().checkTenantPermission(autoRule.getTenantId());
    }

    @PreAuthorize("@pms.hasPermission('/autoRule/cancel')")
    @SysLog(value="逻辑删除自动化规则配置",apiUrl="/autoRule/cancel")
    @Operation(summary = "逻辑删除自动化规则配置")
    @PostMapping(value = "/cancel/{ruleId}")
    public SysResponse<Integer> cancelAutoRule(@PathVariable("ruleId") Integer ruleId){
        log.info("cancel auto rule config id={}", ruleId) ;
        //this.checkRole(ruleId);
        this.appAutoRuleService.cancelAutoRule(ruleId);
        return SysResponse.ok(ruleId,"逻辑删除自动化规则配置成功");
    }


    @PreAuthorize("@pms.hasPermission('/autoRule/del')")
    @SysLog(value="物理删除自动化规则配置",apiUrl="/autoRule/del")
    @Operation(summary = "物理删除自动化规则配置")
    @PostMapping(value = "/del/{ruleId}")
    public SysResponse<Integer> delAutoRule(@PathVariable("ruleId") Integer ruleId){
        log.info("del auto rule config id={}", ruleId) ;
        this.checkRole(ruleId);
        this.appAutoRuleService.delAutoRule(ruleId);
        return SysResponse.ok(ruleId,"物理删除自动化规则配置成功");
    }


    @Operation(summary = "查看自动化规则详细信息")
    @PostMapping(value = "/info/{ruleId}")
    public AutoRuleVo getAutoRule(@PathVariable("ruleId") Integer ruleId){
        log.info(" get auto rule config id=:{}", ruleId);
        return this.appAutoRuleService.getAutoRule(ruleId);
    }

    /**
     * 停用
     */
    @PreAuthorize("@pms.hasPermission('/autoRule/disable')")
    @SysLog(value="停用自动化规则",apiUrl="/autoRule/disable")
    @Operation(summary = "停用自动化规则")
    @PostMapping(value = "/disable/{ruleId}")
    public SysResponse<Integer> disableAutoRule(@PathVariable("ruleId") Integer ruleId){
        log.info("disable auto rule config id={}", ruleId) ;
        this.checkRole(ruleId);
        this.appAutoRuleService.disableAutoRule(ruleId);
        return SysResponse.ok(ruleId,"停用自动化规则成功");
    }
    /**
     * 启用
     */
    @PreAuthorize("@pms.hasPermission('/autoRule/enable')")
    @SysLog(value="启用自动化规则",apiUrl="/autoRule/enable")
    @Operation(summary = "启用自动化规则")
    @PostMapping(value = "/enable/{ruleId}")
    public SysResponse<Integer> enableAutoRule(@PathVariable("ruleId") Integer ruleId){
        log.info("enable auto rule config id={}", ruleId) ;
        this.checkRole(ruleId);
        this.appAutoRuleService.enableAutoRule(ruleId);
        return SysResponse.ok(ruleId,"启用自动化规则成功");
    }

}
